<template>
	<view class="">
		<text>请稍等...</text>
	</view>
</template>

<script>
	import weixinpayapi from '@/util/weixin/weixinpay.js'
	import local_openid from '@/util/weixin/local-openid.js'
	export default ({
		data: function() {
			return {
				myopenid: ''
			}
		},
		onShow:function(){
			// uni.showLoading({
			// 	mask:true
			// });
		},
		onLoad(options) {
			
			let from = options.from_uri
			let to = options.to_uri
			this.getopenid(from, to)
		},
		methods: {
			getopenid(from, to) {
				let that = this;
				let channel_from = '';
				// #ifdef H5
				channel_from = 'H5';
				if (this.$wxbroswor)
					this.getjssdkCode((code) => {
						weixinpayapi.getOpenid(code, channel_from, (_openid) => {
							that.redirectUrl(from, to)
						})
					});
				else that.redirectUrl(from, to)
				// #endif
				// #ifdef MP-WEIXIN
				channel_from = 'MP';
				weixinpayapi.checkLogin_MP((code) => {
					weixinpayapi.getOpenid(code, channel_from, (_openid) => {
						that.redirectUrl(from, to)
					})
				});
				// #endif
			},
			getjssdkCode(callback) {

				let backurl = encodeURIComponent(window.location.href)
				let code = weixinpayapi.getUrlKey('code')
				if (!code) {
					window.location.href =
						'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.$h5appid + '&redirect_uri=' +
						backurl + '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'
				} else {
					if (callback) callback(code)
				}

			},
			redirectUrl(from, to) {

				let timer = setTimeout(() => {

					timer = null

					if (from) {
						uni.navigateTo({
							url: from,
							success: res => {},
							fail: () => {},
							complete: () => {}
						})
					} else if (to) {
						uni.navigateTo({
							url: decodeURIComponent(to),
							success: res => {},
							fail: () => {},
							complete: () => {}
						})
					} else {
						uni.redirectTo({
							url: '/pages/tabBar/index'
						});
					}



				}, 1500);

			}
		}
	})
</script>

<style>
</style>
